# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="MrEction"
__date__ ="$Feb 8, 2011 11:38:20 AM$"
# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="MrEction"
__date__ ="$Jan 27, 2011 4:12:10 PM$"


import pygame
from pygame.draw import *
from hexconfig import *
import random

HEXSIDELEN = 16
GRIDORIGIN = (400.0,300.0)
HEXORIGIN = (GRIDORIGIN[0]+dx(HEXSIDELEN,math.radians(60.0)),
            GRIDORIGIN[1]+dy(HEXSIDELEN,math.radians(60.0)))
LINETHICK = 2

GRIDXLEN = 10
GRIDYLEN = 10
GRIDZLEN = 10


DEBUGCIRCLELOCS = False
class HexWorld():
    
    def __init__(self):
        self.setup()

    def setup(self):

       
        #print SWIDTH,",",SHEIGHT
        self.screen = pygame.display.set_mode((SWIDTH,SHEIGHT))#,pygame.HWSURFACE)
        pygame.display.set_caption("HEXworld")

        pygame.event.set_grab(False)
        self.clock = pygame.time.Clock()
        #pygame.font.initialize()

        self.keyPressDict = {}
        for k in KEYLIST:
            self.keyPressDict[k] = False
        self.debugMode = False


        self.mouseHold = False

        self.state = IDLE

        self.gridSetup()

        #self.discoFlag STUFF
        self.gridanimtick  = 0.0
        self.animGridList = []
        self.animspd = 100
        self.discoFlag = False
        #self.discoFlag STUFF
    """
              ____
          /    \\
     ____/ 0 1-1\\____
         \      //    \
   -1 1 0 \____// 1 0-1\___
          /    \\      /
     ____/ 0 0 0\\____/ 2 -1 -1
         \      //    \
   -1 0 1 \____// 1-1 0\__
          /    \\      /
     ____/ 0-1 1\\____/
         \      //    \
          \____//      \

    the flower down is recursively adding the the bottom,
    until any values hit extremes

    """

    def getPosFromHexLoc(self,hexLoc,drawable = False):
        pos = [HEXORIGIN[0],HEXORIGIN[1]]
        l = HEXSIDELEN


        #X COMPONENT
        pos[0]+=l*hexLoc[0]
        #YCOMPONENT
        pos[0]+=dx(l*hexLoc[1],math.radians(-120.0))
        pos[1]+=dy(l*hexLoc[1],math.radians(-120.0))
        #ZCOMPONENT
        pos[0]+=dx(l*hexLoc[2],math.radians(120.0))
        pos[1]+=dy(l*hexLoc[2],math.radians(120.0))

        if drawable:
            pos = [int(pos[0]),int(pos[1])]
        return pos
    def gridSetup(self):
        self.gridLocs = []
        """
        for x in range(GRIDXLEN):
            self.gridLocs.append([])
            for y in range(GRIDYLEN):
                self.gridLocs[x].append([])
                for z in range(GRIDZLEN):

                    loc = 0
                    self.gridLocs[x][y].append(loc)
        """
        self.gridLocs = self.getFlowerOutGrid()

    def getFlowerOutGrid(self):
        origincoord = (0,0,0)
        coordList = [origincoord]
        self.flowerOutNewHexs(origincoord, coordList)
        print "FlowerOut Successful:"
        return coordList

    def flowerOutNewHexs(self,coord,list):
        extreme = 5
        for val in coord:
            if math.fabs(val)>extreme:
                return
        newcoords = []

        newcoords.append((coord[0]-1,coord[1],coord[2]+1))
        newcoords.append((coord[0],coord[1]-1,coord[2]+1))
        newcoords.append((coord[0]+1,coord[1]-1,coord[2]))
        newcoords.append((coord[0]-1,coord[1]+1,coord[2]))
        newcoords.append((coord[0],coord[1]+1,coord[2]-1))
        newcoords.append((coord[0]+1,coord[1],coord[2]-1))
        for nc in newcoords:
            if not nc in list:
                list.append(nc)
                self.flowerOutNewHexs(nc,list)


    def getFlowerDownGrid(self):
        origincoord = (0,0,0)
        coordList = [origincoord]
        self.flowerDownNewHexs(origincoord, coordList)
        print "FlowerDown Successful:"
        return coordList

    def flowerDownNewHexs(self,coord,list):
        extreme = 20
        for val in coord:
            if math.fabs(val)>extreme:
                return
        newcoords = []

        newcoords.append((coord[0]-1,coord[1],coord[2]+1))
        newcoords.append((coord[0],coord[1]-1,coord[2]+1))
        newcoords.append((coord[0]+1,coord[1]-1,coord[2]))

        for nc in newcoords:
            if not nc in list:
                list.append(nc)
                self.flowerDownNewHexs(nc,list)

    #def getHex
    def drawHexGrid(self,surf):
        for loc in self.gridLocs:
            self.drawHexTop(surf,self.getPosFromHexLoc(loc),False)
        return

    def drawHexTop(self,surf,loc,topleft = True):
        l = HEXSIDELEN
        if topleft:
            pts = [(),(),(),()]
            pts[0] = loc
            pts[1] = (loc[0]+l,loc[1])
            pts[2] = (pts[1][0]+dx(l,math.radians(60.0)),pts[1][1]+dy(l,math.radians(60.0)))
            pts[3] = (pts[0][0]+dx(l,math.radians(120.0)),pts[0][1]+dy(l,math.radians(60.0)))

            for pt in pts:
                pt = (int(pt[0]),int(pt[1]))
            line(surf,(0,0,0),pts[0],pts[1],LINETHICK)
            line(surf,(0,0,0),pts[1],pts[2],LINETHICK)
            line(surf,(0,0,0),pts[0],pts[3],LINETHICK)
        else:
            
            topleftpt = (loc[0]+dx(l,math.radians(-120.0)),loc[1]+dy(l,math.radians(-120.0)))
            self.drawHexTop(surf, topleftpt, True)
        return

    def drawHexFill(self,surf,color,loc,topleft = True):
        l = HEXSIDELEN
        if not topleft:
            topleftpt = (loc[0]+dx(l,math.radians(-120.0)),loc[1]+dy(l,math.radians(-120.0)))
            self.drawHexFill(surf,color, topleftpt, True)
        else:
            pts = [(),(),(),(),(),()]
            pts[0] = loc
            pts[1] = (loc[0]+l,loc[1])
            pts[2] = (pts[1][0]+dx(l,math.radians(60.0)),pts[1][1]+dy(l,math.radians(60.0)))
            pts[3] = (pts[2][0]+dx(l,math.radians(120.0)),pts[2][1]+dy(l,math.radians(120.0)))
            pts[4] = (pts[3][0]-l,pts[3][1])
            pts[5] = (pts[0][0]+dx(l,math.radians(120.0)),pts[0][1]+dy(l,math.radians(60.0)))
            bpts = []
            for pt in pts:
                pt = (int(pt[0]),int(pt[1]))
                bpts.insert(0,pt)
            polygon(surf,color,pts)
    def tick(self):
        etime = self.clock.tick()
        for e in pygame.event.get():
            self._handle_event(e)
        self.updateAll(etime)
        self.drawAll(self.screen)

        return


    
    def updateAll(self,etime):
        if self.discoFlag:
            self.discoUpdate(etime)

        return
        

    def checkMouse(self,pos):
        return


    def drawAll(self,surf):

        surf.fill((255,255,255))

        if self.discoFlag:
            for hexloc in self.animGridList:
                self.drawHexFill(surf, hexloc[1], self.getPosFromHexLoc(hexloc[0],True), False)
       
        if DEBUGCIRCLELOCS:
            for hexloc in self.gridLocs:
               circle(surf,(255,0,0),self.getPosFromHexLoc(hexloc,True),2)
        self.drawHexGrid(surf)
        
        pygame.display.flip()

    def _handle_event(self,event):
        #super._handle_event(self,event)
        if event.type == pygame.QUIT:
            pygame.quit()
            #raise EndOfGameException("Quit")
            exit(0)
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                exit(0)
                #raise EndOfGameException("Quit")
            if event.key == pygame.K_d:
                self.discoFlag = not self.discoFlag
            if event.key == pygame.K_RETURN:
                self.executeJumps()
            if event.key == pygame.K_DOWN:
                self.keyPressDict['DOWN'] = True
            if event.key == pygame.K_UP:
                self.keyPressDict['UP'] = True
            if event.key == pygame.K_LEFT:
                self.keyPressDict['LEFT'] = True
            if event.key == pygame.K_RIGHT:
                self.keyPressDict['RIGHT'] = True
            if event.key == pygame.K_s:
                self.keyPressDict['DOWN'] = True
            if event.key == pygame.K_w:
                self.keyPressDict['UP'] = True
            if event.key == pygame.K_a:
                self.keyPressDict['LEFT'] = True
            if event.key == pygame.K_d:
                self.keyPressDict['RIGHT'] = True
            if event.key == pygame.K_j:
                self.testCalcJumpDisp()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                self.keyPressDict['DOWN'] =False
            if event.key == pygame.K_UP:
                self.keyPressDict['UP'] = False
            if event.key == pygame.K_LEFT:
                self.keyPressDict['LEFT'] = False
            if event.key == pygame.K_RIGHT:
                self.keyPressDict['RIGHT'] =False
            if event.key == pygame.K_s:
                self.keyPressDict['DOWN'] = False
            if event.key == pygame.K_w:
                self.keyPressDict['UP'] = False
            if event.key == pygame.K_a:
                self.keyPressDict['LEFT'] = False
            if event.key == pygame.K_d:
                self.keyPressDict['RIGHT'] = False

        if event.type== pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                self.mouseHold = True
                self.checkMouse(pygame.mouse.get_pos())

        if event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                self.mouseHold = False

        if event.type == pygame.MOUSEMOTION:
            #if self.mouseHold:
            #    self.checkMouse(pygame.mouse.get_pos())
            pass

    def discoUpdate(self,etime):
        self.gridanimtick+=etime
        if self.gridanimtick>self.animspd:
            self.animGridList = random.sample(self.gridLocs,random.randrange(20,70))
            for i in range(len(self.animGridList)):
                self.animGridList[i]=(self.animGridList[i],(random.choice(RAINBOW2)))
            self.gridanimtick = 0.0
            self.animspd +=random.randrange(-5,10)
            if self.animspd<25 or self.animspd>175:
                self.animspd = 100
if __name__ == "__main__":

    engine = HexWorld()
    print "welcome,sit down and stay for a while(True)"
    while(True):
        engine.tick()



